#include "MGraph_int.h"

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

MGraph mgraph_create(int size) {
	assert(size > 0);
	MGraph g;
	g = (MGraph)malloc(sizeof(_MGraph));
	assert(g);
	g->size = size;
	int size2 = size * size;
	g->data = (MGraphElementType*)malloc(sizeof(MGraphElementType) * size2);
	MGraphElementType* data = g->data;
	assert(data);
	for (int i = 0; i < size2; i++)
		* data++ = -1;
	return g;
}

MGraph mgraph_free(MGraph g) {
	assert(g);
	return g;
}

MGraph mgraph_add_vertex(MGraph g, int v0, int v1) {
	assert(g);
	assert(v0 != v1);
	assert(0 <= v0 && v0 < g->size);
	assert(0 <= v1 && v1 < g->size);

	g->data[v0 * g->size + v1] = 1;
	g->data[v1 * g->size + v0] = 1;
	return g;
}

MGraphElementType mgraph_at(MGraph g, int row, int col) {
	assert(g);
	assert(0 <= row && row < g->size);
	assert(0 <= col && col < g->size);

	return g->data[row * g->size + col];
}

void print_mgraph(MGraph g) {
	int j;
	assert(g);
	assert(g->size > 0);
	MGraphElementType* data = g->data;
	printf("%5s", "");
	for (j = 0; j < g->size; j++)
		printf("%4d", j);
	printf("\n -- \n");
	for (int i = 0; i < g->size; i++) {
		printf("%3d |", i);
		for (j = 0; j < g->size; j++)
			printf("%4d", *data++);
		printf("\n");
	}
}